package com.ymatou.operate.trading.api.testcase;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.trading.operate.parameter.PlaceOrderResp;
import com.ymatou.operate.trading.api.parameter.ChangeOrderDeliveryNumberBean;
import com.ymatou.operate.trading.api.service.ChangeOrderDeliveryNumberCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.constant.ECode;

/**
 * 修改物流单号 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_ChangeOrderDeliveryNumber {

	private static ChangeOrderDeliveryNumberBean codnbean;
	private static ChangeOrderDeliveryNumberCall codncall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("修改物流单号");
	}

	@Before
	public void caseUp() {

		codnbean = new ChangeOrderDeliveryNumberBean();
		codncall = new ChangeOrderDeliveryNumberCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(codncall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeOrderDeliveryNumber_001() {
		Logger.start(true,"修改物流单号成功");
		try {
			// 创建订单
			Logger.comment("前置数据准备");		
			PlaceOrderResp placeOrderResp=new OrderCallService().createM2cOrder();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId =placeOrderResp.getOrderList().get(0).getSellerId();
			int userId =placeOrderResp.getUserId();         
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal totalPrice =  placeOrderResp.getPayAmount();// 支付金额
			// 支付订单
			new OrderCallService().orderPayRequest(userId, totalPrice, tradingId);			
			// 接单
			new OrderCallService().acceptOrder(orderId, sellerId);			
			// 商家发货
			new OrderCallService().dispatched(orderId, sellerId);
			Logger.comment("前置数据准备结束");
			String deliverSummary = TestDataManager.randomStr(10);
			codnbean.setBillCodeList(deliverSummary);
			codnbean.setOrderId(orderId);
			codnbean.setSellerId(sellerId);
			// 发送请求
			codncall.setData(codnbean);
			codncall.callService();
			Logger.verifyEquals(ECode.SUCCESS, codncall.getCode(), "验证Code");

			// 数据库验证
			DefaultWapper database = new DefaultWapper();
			HashMap m = new HashMap();
			m.put("OrderId", orderId);
			m.put("Summary", deliverSummary);
			Logger.verifyEquals(1, database.selectOrderSummary(m).size(), "验证更新为新物流单号");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_ChangeOrderDeliveryNumber_002() {
		Logger.start(false,"订单号不存在");
		try {
			String deliverSummary = TestDataManager.randomStr(10);
			int orderId = 123;
			codnbean.setBillCodeList(deliverSummary);
			codnbean.setOrderId(orderId);
			codnbean.setSellerId(222484);
			// 发送请求
			codncall.setData(codnbean);
			codncall.callService();
			Logger.verifyEquals(ECode.FAILED201, codncall.getCode(), "验证Code");
			Logger.verifyEquals("订单号" + orderId + "不存在", codncall.getMsg(), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
    @Category(P3.class)
    @TestCase
    public void Tc_ChangeOrderDeliveryNumber_003() {
        Logger.start(false,"操作用户id为null");
        try {
            String deliverSummary = TestDataManager.randomStr(10);
            int orderId = 123;
            codnbean.setBillCodeList(deliverSummary);
            codnbean.setOrderId(orderId);
            //codnbean.setSellerId(222484);
            // 发送请求
            codncall.setData(codnbean);
            codncall.callService();
            Logger.verifyEquals(ECode.ERROR400, codncall.getCode(), "验证Code");
            Logger.verifyEquals(ECode.ERROR400_MSG_12, codncall.getMsg(), "验证Msg");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }
	@Test
    @Category(P3.class)
    @TestCase
    public void Tc_ChangeOrderDeliveryNumber_004() {
        Logger.start(false,"订单号为null");
        try {
            String deliverSummary = TestDataManager.randomStr(10);
            int orderId = 123;
            codnbean.setBillCodeList(deliverSummary);
            //codnbean.setOrderId(orderId);
            codnbean.setSellerId(222484);
            // 发送请求
            codncall.setData(codnbean);
            codncall.callService();
            Logger.verifyEquals(ECode.ERROR400, codncall.getCode(), "验证Code");
            Logger.verifyEquals(ECode.ERROR400_MSG_14, codncall.getMsg(), "验证Msg");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }
	@Test
    @Category(P3.class)
    @TestCase
    public void Tc_ChangeOrderDeliveryNumber_005() {
        Logger.start(false,"物流单号为null");
        try {
            String deliverSummary = TestDataManager.randomStr(10);
            int orderId = 123;
            //codnbean.setBillCodeList(deliverSummary);
            codnbean.setOrderId(orderId);
            codnbean.setSellerId(222484);
            // 发送请求
            codncall.setData(codnbean);
            codncall.callService();
            Logger.verifyEquals(ECode.ERROR400, codncall.getCode(), "验证Code");
            Logger.verifyEquals(ECode.ERROR400_MSG_38, codncall.getMsg(), "验证Msg");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }
}